/******************************************************************
 *
 *    Java Lib For Android, Powered By GuangZhou WLB.
 *
 *    Copyright (c) 2001-2014 Digital Telemedia Co.,Ltd
 *    http://www.d-telemedia.com/
 *
 *    Package:     com.example.imagepicker.fragment
 *
 *    Filename:    FragmentPhoto.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2001-2014
 *
 *    Company:     Digital Telemedia Co.,Ltd
 *
 *    @author: WLB
 *
 *    @version: 1.0.0
 *
 *    Create at:   2016年4月20日 下午2:49:41
 *
 *    Revision:
 *
 *    2016年4月20日 下午2:49:41
 *        - first revision
 *
 *****************************************************************/
package com.eboss2.sbs.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;

import com.eboss2.sbs.R;
import com.example.imagepicker.customviews.photoview.PhotoView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.tool.utils.utils.ToastUtils;


/**
 * @author WLB
 * @ClassName FragmentPhoto
 * @category 所属功能分类
 * @Date 2016年4月20日 下午2:49:41
 * @Description what
 */
public class FragmentPhoto extends Fragment {
    private View view;
    public static final String KEY_PATH = "path";
    private String path;

//    public FragmentPhoto(String path){
//        this.path = path;
//    }
//
//    public FragmentPhoto(){
//        this.path = "";
//    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.from(getActivity()).inflate(R.layout.photoview, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Bundle fragmentArgs = getArguments();
        path = fragmentArgs.getString(KEY_PATH, "");
        PhotoView photoView = (PhotoView) view.findViewById(R.id.photoview);

        photoView.setScaleType(ScaleType.FIT_CENTER);
        if (!ImageLoader.getInstance().isInited()) {
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getActivity()).build();
            ImageLoader.getInstance().init(config);
        }

        ImageLoader.getInstance().displayImage(path, photoView, new ImageLoadingListener() {

            @Override
            public void onLoadingStarted(String arg0, View arg1) {

            }

            @Override
            public void onLoadingFailed(String arg0, View arg1, FailReason failReason) {
                Log.e("FragmentPhoto", "图片加载出错");
                String message = "";
                switch (failReason.getType()) { // 获取图片失败类型
                    case IO_ERROR: // 文件I/O错误
                        message = "文件I/O错误";
                        break;
                    case DECODING_ERROR: // 解码错误
                        message = "解码错误";
                        break;
                    case NETWORK_DENIED: // 网络延迟
                        message = "Downloads are denied";
                        break;
                    case OUT_OF_MEMORY: // 内存不足
                        message = "内存不足";
                        break;
                    case UNKNOWN: // 原因不明
                        message = "Unknown error";
                        break;
                }
                ToastUtils.showShort(getActivity(), message);
            }

            @Override
            public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {

            }

            @Override
            public void onLoadingCancelled(String arg0, View arg1) {

            }
        });

    }

}
